package com.demo.modules.job.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;

/** mysql 数据备份 mysqldump 为命令 */
@Component("mySqlBackUpDataBaseTask")
@Slf4j
public class MySqlBackUpDataBaseTask implements ITask {

  @Value("${spring.datasource.druid.driver-class-name}")
  private String driverClassName;

  @Value("${spring.datasource.druid.url}")
  private String url;

  @Value("${spring.datasource.druid.username}")
  private String userName;

  @Value("${spring.datasource.druid.password}")
  private String password;

  @Value("${fileupload.path}")
  private String sqlPath;

  @Override
  public void run(String params) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
    String format = dateFormat.format(System.currentTimeMillis());
    exportSql(format);
  }

  /** 获取数据库名 */
  public String getDataBaseName() {
    return url.substring(url.indexOf("3306"), url.indexOf("?"))
        .replaceAll("/", "")
        .replaceAll("3306", "");
  }

  /** 获取主机地址 */
  private String getHost() {
    return url.substring(url.indexOf("mysql"), url.indexOf("3306"))
        .replace(":", "")
        .replace("//", "")
        .replace("mysql", "");
  }

  /** 导出 sql 并返回相关信息 */
  public void exportSql(String time) {
    // 指定导出的 sql 存放的文件夹
    File saveFile = new File(sqlPath + "/sql/");
    if (!saveFile.exists()) {
      saveFile.mkdirs();
    }

    String host = getHost();
    String dataBaseName = getDataBaseName();
    String fileName = time + "_" + dataBaseName + ".sql";

    StringBuilder sb = new StringBuilder();
    // 拼接备份命令
    sb.append("mysqldump")
        .append(" --opt")
        .append(" -h ")
        .append(host)
        .append(" --user=")
        .append(userName)
        .append(" --password=")
        .append(password);
    sb.append(" --result-file=")
        .append(sqlPath + "/sql/" + fileName)
        .append(" --default-character-set=utf8 ")
        .append(dataBaseName);

    try {
      Process exec = Runtime.getRuntime().exec(sb.toString());
      if (exec.waitFor() == 0) {
        log.info("数据库备份成功，保存路径：" + sqlPath);
      } else {
        System.out.println("process.waitFor()=" + exec.waitFor());
      }
    } catch (IOException e) {
      log.error("备份 数据库 出现 IO异常 ", e);
    } catch (InterruptedException e) {
      log.error("备份 数据库 出现 线程中断异常 ", e);
    } catch (Exception e) {
      log.error("备份 数据库 出现 其他异常 ", e);
    }
  }
}
